//
//  ChangePhoneVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/11.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "ChangePhoneVC.h"
#import "LogInVC.h"

@interface ChangePhoneVC ()
@property (copy, nonatomic) NSArray *arrTitles;
@property (strong, nonatomic) NSArray *arrContent;
@property (strong, nonatomic) CJButton *btnSave;
@end

@implementation ChangePhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"修改电话"];
    [self setBackLeftButton:@""];
    [self createUI];
    [self.view addSubview:self.btnSave];
}

- (void)createUI{
    
    for (int i = 0; i < self.arrTitles.count; i++) {
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0,kTopHeight + CJToViewMarginH+ i * LW(50), SCREEN_WIDTH, LW(50))];
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];

        CJLabel *label = CJLabel.labInit
        .labFrame(CGRectMake(CJToLeftMargin, LW(5), LW(150), LW(40)))
        ;
        [liebiaoView addSubview:label];
        [label setAnotherColor:[UIColor cjRedColor]];
        [label setAnotherTest:CJStringWithFormat(@"<*>%@ ",self.arrTitles[i])];
       
        UITextField *textField = [UITextField textFieldWithFrame:CGRectMake(label.right, label.top, SCREEN_WIDTH - CJToLeftMargin-label.width - LW(25), LW(40)) placeholder:self.arrContent[i] backgroundColor:[UIColor cjClearColor] borderColor:nil cornerRadius:0 font:[UIFont cjTitleFont14] textColor:[UIColor cjMainTextColor] textAlignment:NSTextAlignmentRight text:@"" delegate:nil];
        textField.tag = 1000+ i;
        [liebiaoView addSubview:textField];
//        textField.secureTextEntry = YES;
    
        if (i == 0) {
            textField.text = self.arrContent[i];
            textField.enabled = NO;
        } else if(i == 3) {
            textField.secureTextEntry = YES;
        }
   
//        UIImageView * right = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-CJToLeftMargin-LW(18), LW(15), LW(18), LW(18))];
//        right.image = [UIImage imageNamed:@"zcj_arrow_right"];
//        right.contentMode = UIViewContentModeScaleAspectFit;
//        [liebiaoView addSubview:right];
//        if (i == 0) {
//               right.hidden = YES;
//           }
        
        UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
        bg.backgroundColor = [UIColor cjBackgroundColor];
        [liebiaoView addSubview:bg];
        
    }
    
}


- (void)btnSaveAction{
    
    UITextField *filed = [self.view viewWithTag:1000];
    UITextField *filed1 = [self.view viewWithTag:1001];
    UITextField *filed2 = [self.view viewWithTag:1002];
    UITextField *filed3 = [self.view viewWithTag:1003];

    if (IsStrEmpty(filed1.text)) {
        [[[AFViewShaker alloc] initWithView:filed1] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"请填写新手机号"];
            
        }];
    }else if (IsStrEmpty(filed2.text)){
        [[[AFViewShaker alloc] initWithView:filed2] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"请填写新手机号"];
            
        }];
    }else if (![filed2.text isEqualToString:filed1.text]){
        [[[AFViewShaker alloc] initWithView:filed2] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"两次手机号填写不一致，请重新填写"];
            
        }];
        [[[AFViewShaker alloc] initWithView:filed1] shakeWithDuration:[ShakerTime floatValue] completion:^{
           
        }];
    }else if (IsStrEmpty(filed3.text)){
        [[[AFViewShaker alloc] initWithView:filed3] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"请填写密码"];
            
        }];
   
    }else{
            
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/party/updatePhone",CJBaseUrl);

        NSDictionary *dicPra = @{@"oldPhone":filed.text,
                                 @"newPhone":filed2.text,
                                 @"pwd":[CJControl base64:filed3.text],
                                 @"branchCode":[CJControl getBranchCode]
                                };
        @weakify(self);
        [[HDNetworking sharedHDNetworking] POST:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
            @strongify(self);
            DLog(@"++++++++%@",responseObject);
//            [ws showLoginWindow];
            
//            LogInVC *vc = [[LogInVC alloc]init];
//            vc.isRegsiter = YES;
//            [ws pushVc:vc];
     
            [self pop];

        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);

        }];
    }

}

#pragma mark ----------lazy
- (CJButton *)btnSave{
    if (!_btnSave) {
        _btnSave = CJButton.btnInit
        .btnFrame(CGRectMake(2*CJToLeftMargin, (LW(50) + CJToViewMarginH )*self.arrTitles.count + kTopHeight, SCREEN_WIDTH - 4*CJToLeftMargin, LW(50)))
        .btnFont([UIFont cjTitleFont17])
        .btnText(@"确认提交")
        .btnBgColor([UIColor cjMainColor])
        .btnCornerRadius(5)
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnTargetAction(self, @selector(btnSaveAction))
        ;
    }
    return _btnSave;
}

- (NSArray *)arrContent{
    if (!_arrContent) {
        _arrContent = @[self.strPhone,@"请填写新的手机号",@"请确认新的手机号",@"请填写密码"];
    }
    return _arrContent;
}

- (NSArray *)arrTitles{
    if (!_arrTitles) {
        _arrTitles = @[@"原手机号",@"新手机号",@"确认新手机号",@"请填写密码"];
    }
    return _arrTitles;
}


@end
